//
//  HXCycleImages.m
//  06_CycleImages
//
//  Created by 徐昊 on 16/7/21.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "HXCycleImages.h"

@interface HXCycleImages ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *imagesPage;
@end

@implementation HXCycleImages

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];
        // 子控件初始化时, frame不要与初始化方法的frame一样(造成错位)
        // 推荐使用self.bounds 或者 CGRrectMake(0, 0, frame.size.width, frame.size.height)
        [self initCycleScrollView];
        [self createPageControl];
    }
    return self;
}
- (void)initCycleScrollView {
    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.backgroundColor = [UIColor greenColor];
    [self addSubview:_cycleScrollView];
    _cycleScrollView.pagingEnabled= YES;
    _cycleScrollView.bounces = NO;
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    _cycleScrollView.delegate = self;
}
// 后期需要添加的page的位置改变, 这里默认位置
- (void)createPageControl {
    _imagesPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width / 3, 20)];
    _imagesPage.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height - 10);
    _imagesPage.backgroundColor = [UIColor redColor];
    _imagesPage.numberOfPages = 5;
    [self addSubview:_imagesPage];
    [_imagesPage addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
}
#pragma mark - ImageArray
- (void)setImagesWithArray:(NSArray *)array {
    NSMutableArray *imageArray = [self handleArray:array];
    // 逻辑判断时, 需要用到数组内容
    self.imagesArray = [NSArray arrayWithArray:imageArray];
    // 使用数组创建ImageView
    [self createAllImageViewWithImageArray:imageArray];
    _imagesPage.numberOfPages = array.count;
    _cycleScrollView.contentSize = CGSizeMake(imageArray.count * self.bounds.size.width, 0);
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
}
- (__kindof NSArray * )handleArray:(NSArray *)array {
    // 判断类型(不知道别人使用你的类的传进来的类型是什么)
    id firstObject = [array firstObject];
    // 最终图片的数组
    NSMutableArray *imageArray = [NSMutableArray array];
    // 判断对象是不是某种类型 isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串数组转成图片数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imageArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        [imageArray setArray:array];
    }
    // 获取到第一张图
    UIImage *firstImage = [imageArray firstObject];
    // 获取到最后一张图
    UIImage *lastImage = [imageArray lastObject];
    // 下标为0的位置放最后一张图
    [imageArray insertObject: lastImage atIndex:0];
    // 在最后添加第一张图
    [imageArray addObject:firstImage];
    return [imageArray copy];
}
- (void)createAllImageViewWithImageArray:(NSArray *)imageArray {
    for (int i = 0; i < imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imageArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page = _cycleScrollView.contentOffset.x / self.bounds.size.width;
    _imagesPage.currentPage = page - 1;
    if (page == 0) {
        // 改变滚动视图的偏移量
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (self.imagesArray.count - 2), 0);
        // 改变pageControl的点位置
        _imagesPage.currentPage = _imagesArray.count - 1;
    } if (page == _imagesArray.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _imagesPage.currentPage = 0;
    }
}
- (void)pageChange:(UIPageControl *)page {
    [_cycleScrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
}


@end
